package fr.mywiki.view.array.cell.entry;

import fr.mywiki.business.entry.EntryLite;
import fr.mywiki.business.entry.EntryManager;
import fr.mywiki.business.wiki.WikiWordTokenizer;
import fr.mywiki.model.ejbAdapter.Value;
import fr.mywiki.view.array.cell.CellType;

/**
 * Displays the n first words of the simple text representation of a wiki text.
 */
public class WikiEntryDisplayer implements CellType {
	/** Number of words to display. */
	private int words;

	public WikiEntryDisplayer(int words) {
		this.words = words;
	}

	public String compute(Value value) {
		try {
			EntryLite entry = (EntryLite) value;
			String content = EntryManager.getCurrentContent(entry);
			WikiWordTokenizer tokenizer = new WikiWordTokenizer(content);
			StringBuffer displayed = new StringBuffer();
			int i = 0;
			while (i < words && tokenizer.hasMoreWords()) {
				displayed.append(tokenizer.nextWord() + " ");
				i++;
			}
			return displayed.toString()+ " ...";
		} catch (Exception e) {
			return new String();
		}
	}
}
